import { UploadFile } from "antd";
import { request } from "../request/request";
import { User } from "../Types/User";

const mock = false;

const prefix = `${mock ? "/mock" : "/sys_user"}`;

export const getUsers = (params: (ListRequest & User) | ListRequest) =>
    request({
        url: `${prefix}/get_users`,
        method: "get",
        params: params,
    }) as Promise<ListResponse<User>>;

export const getUser = (id: number) =>
    request({
        url: `/getUserById`,
        method: "get",
        params: {
            id,
        },
    }) as Promise<BaseResponse<User>>;

export const createUser = (user: User) =>
    request({
        url: `${prefix}/create_user`,
        method: "post",
        data: user,
    });

export const updateUser = (id: number, user: User) =>
    request({
        url: `${prefix}/update_user`,
        method: "put",
        data: {
            ...user,
            id,
        },
    });

export const removeUser = (ids: number[]) =>
    request({
        url: `${prefix}/remove_user`,
        method: "delete",
        data: { ids },
    });

export const uploadSysUserAvatar = (file: UploadFile) => {
    const data = new FormData();
    if (file.originFileObj instanceof File) {
        data.append("file", new File([file.originFileObj], file.name, {}));
    }
    return request({
        url: `/files/upload_sys_user_avatar`,
        method: "PUT",
        data,
    });
};
